﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BUS;


public partial class ForgotPassword : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["user"] != null)
            ScriptManager.RegisterStartupScript(Page, Page.GetType(), "alert", "alert('Bạn đã đăng nhập rồi');window.location='Default.aspx';", true);
    }

    protected void btnSumit_Click(object sender, EventArgs e)
    {
        switch (MultiView1.ActiveViewIndex)
        {
            case 0:
                ProcessUserName();
                break;
            case 1:
                ProcessQuestion();
                break;
            case 2:
                ProcessChangePass();
                break;
        }
    }

    private void ProcessChangePass()
    {
        App_Code.Account.ChangePassWord(txtNhapLaiMatKhau.Text);
        Session["user"] = App_Code.Account.KhachHang.TenDangNhap;
        Response.Redirect("~/AccountDetails.aspx?user=" + App_Code.Account.KhachHang.TenDangNhap.EncryptMD5());
    }

    private void ProcessQuestion()
    {
        if (App_Code.Account.KhachHang.TraLoi == txtTraLoi.Text)
        {
            lblTenDangNhap1.Text = App_Code.Account.KhachHang.TenDangNhap;
            MultiView1.SetActiveView(ViewChangePass);
        }
        else
        {
            Alert("Câu trả lời chưa đúng...");
            MultiView1.SetActiveView(ViewSecretQuestion);
        }
    }

    private void ProcessUserName()
    {
        if (App_Code.Account.GetNameByUserName(txtTenDangNhap.Text))
        {
            lblTenDangNhap.Text = App_Code.Account.KhachHang.TenDangNhap;
            lblCauHoi.Text = App_Code.Account.KhachHang.CauHoi;
            MultiView1.SetActiveView(ViewSecretQuestion);
        }
        else
        {
            string msg = "Tên đăng nhập chưa đúng";
            Alert(msg);
        }
    }

    private void Alert(string msg)
    {
        ScriptManager.RegisterStartupScript(Page, Page.GetType(), "alert", "alert('" + msg + "');", true);
        MultiView1.SetActiveView(ViewUserName);
    }
}